package com.mvity.framework.common.utils;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Json 工具类
 * 
 * @author vity
 * @date 2012-9-9
 * @mail vity.1215@gmail.com
 */
public final class JsonUtil {

	private volatile static ObjectMapper mapper = null;
	private volatile static JsonFactory jsonFactory = null;

	static {
		synchronized (ObjectMapper.class) {
			if (mapper == null) {
				mapper = new ObjectMapper();
			}
			if (jsonFactory == null) {
				jsonFactory = new JsonFactory();
			}
		}
		mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, false);
	}

	private JsonUtil() {
	}

	/**
	 * 转换Java Bean 为 json
	 */
	public static String beanToJson(Object o) {
		StringWriter sw = new StringWriter(300);
		JsonGenerator jsonGenerator = null;

		try {
			jsonGenerator = jsonFactory.createJsonGenerator(sw);
			mapper.writeValue(jsonGenerator, o);
			return sw.toString();

		} catch (Exception e) {
			return null;

		} finally {
			if (jsonGenerator != null) {
				try {
					jsonGenerator.close();
				} catch (Exception e) {

				}
			}
		}
	}

	/**
	 * json 转 javabean
	 * 
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Object jsonToBean(String json, @SuppressWarnings("rawtypes") Class clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 转换Java Bean 为 HashMap
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> beanToMap(Object o) {
		try {
			return mapper.readValue(beanToJson(o), HashMap.class);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 转换Json String 为 HashMap
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> jsonToMap(String json, boolean collToString) {
		try {
			Map<String, Object> map = mapper.readValue(json, HashMap.class);
			if (collToString) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (entry.getValue() instanceof Collection || entry.getValue() instanceof Map) {
						entry.setValue(beanToJson(entry.getValue()));
					}
				}
			}
			return map;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * List 转换成json
	 * 
	 * @param list
	 * @return
	 */
	public static String listToJson(List<Map<String, String>> list) {
		JsonGenerator jsonGenerator = null;
		StringWriter sw = new StringWriter();
		try {
			jsonGenerator = jsonFactory.createJsonGenerator(sw);
			new ObjectMapper().writeValue(jsonGenerator, list);
			jsonGenerator.flush();
			return sw.toString();
		} catch (Exception e) {
			return null;
		} finally {
			if (jsonGenerator != null) {
				try {
					jsonGenerator.flush();
					jsonGenerator.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * json 转List
	 * 
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> jsonToList(String json) {
		try {
			if (json != null && !"".equals(json.trim())) {
				JsonParser jsonParse = jsonFactory.createJsonParser(new StringReader(json));

				ArrayList<Map<String, String>> arrayList = (ArrayList<Map<String, String>>) new ObjectMapper().readValue(jsonParse, ArrayList.class);
				return arrayList;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}
}
